<template>
    <div class="cube" :style="{
      backgroundColor: config.pageConfig.color,
    }">
        <div :style="{
          marginTop: config.pageConfig.marginTop + 'px',
          marginBottom: config.pageConfig.marginBottom + 'px',
          marginLeft:config.pageConfig.margin + 'px',
          marginRight:config.pageConfig.margin + 'px',
        }">
            <div class="decorate-cube" :style="{
                    width:`calc(326px - ${2*config.pageConfig.margin}px)`,
                    height:`calc(326px - ${2*config.pageConfig.margin}px)`,
                }" id='decorate-cube'>
                <div class="cube-selected" v-for="(item, index) in config.pageConfig.cudeSelected" :key="index"
                    :style="{'width':getCubeSelectedWidth(item)+'px','height':getCubeSelectedHeight(item)+'px','top':getCubeSelectedTop(item)+'px','left':getCubeSelectedLeft(item)+'px'}"
                    @click="tolink(item.url)"
                    >
                    <div :style="{
                        backgroundImage:'url('+item.imageUrl+')',
                        'width':getCubeSelectedWidth(item)+'px',
                        'height':getCubeSelectedHeight(item)+'px',
                        borderTopLeftRadius: config.pageConfig.radiusTop + 'px',
                        borderTopRightRadius: config.pageConfig.radiusTop + 'px',
                        borderBottomLeftRadius: config.pageConfig.radiusBottom + 'px',
                        borderBottomRightRadius: config.pageConfig.radiusBottom + 'px',
                    }" class='banner-image' v-if="item.imageUrl"> </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script lang="ts">
export default {
    name: "Cube",
};
</script>
<script lang="ts" setup>
import { reactive, onMounted } from "vue";
const config = defineProps({
    pageConfig: {
        type: Object,
        default: {
            color: '',
            marginTop: 0,
            marginBottom: 0,
            marginImg:0,
            margin:0,
            radiusTop: 0,
            radiusBottom: 0,
            cudeSelected: []
        },
    },
    show: {
    type: Boolean,
    default: true,
  },
});
const cubeWidth = ref(0); //魔方宽度
const cubeHeight = ref(0); //魔方高度
const densityNum = computed(() => {
    return 4;
});
const cubeItemHeight = computed(() => {
    return cubeWidth.value / densityNum.value;
});
const cubeItemWidth = computed(() => {
    return cubeWidth.value / densityNum.value;
});
const getCubeSelectedWidth = (item) => {
    return (parseInt(item.end.y) - parseInt(item.start.y) + 1) * cubeItemWidth.value - config.pageConfig.marginImg/2;
};
//计算选中层的高度
const getCubeSelectedHeight = (item) => {
    return (parseInt(item.end.x) - parseInt(item.start.x) + 1) * cubeItemHeight.value - config.pageConfig.marginImg/2;
};

//计算选中层的右边距离
const getCubeSelectedTop = (item) => {
    return (item.start.x - 1) * cubeItemHeight.value + config.pageConfig.marginImg/4;
};
//计算选中层的左边距离
const getCubeSelectedLeft = (item) => {
    return (item.start.y - 1) * cubeItemWidth.value + config.pageConfig.marginImg/4;
};
const tolink = (url: string) => {
  if (url && config.show == false) {
    window.location.href = url;
  }
};
watch(
    () => config.pageConfig.margin,
    (val) => {
        const myimage = document.getElementById("decorate-cube"); 
        console.log('myimage',myimage)
        cubeWidth.value = myimage.clientWidth;
        cubeHeight.value = myimage.clientHeight;
        console.log('cubeWidth',cubeWidth.value)
        console.log('cubeHeight',cubeHeight.value)
    }
);
let emits = defineEmits<{ (e: "close", flag: boolean): void }>();
onMounted(() => {
    const myimage = document.getElementById("decorate-cube"); 
    console.log('myimage',myimage)
    cubeWidth.value = myimage.clientWidth;
    cubeHeight.value = myimage.clientHeight;
    console.log('cubeWidth',cubeWidth.value)
    console.log('cubeHeight',cubeHeight.value)
});
</script>
<style lang="scss" scoped>
.cube {
  width: 100%;
  height: 100%;
  overflow: hidden;
  bottom: 0;
}
.decorate-cube {
    position: relative;
}

.decorate-cube .cube-row:last-of-type .cube-item {
    border-right: 1px solid #e5e5e5;
}

.decorate-cube .cube-selected {
    position: absolute;
    background-color: transparent;
    text-align: center;
    color: #7d7e80;
    cursor: pointer;
    box-sizing: border-box;
    overflow: hidden;
}

// 添加的图片
.cube-selected .banner-image {
    width: 100%;
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50%;
}

.decorate-cube .cube-selected.cube-selected-click {
    background: #e0edff;
    border: 1px solid #155bd4;
    color: #155bd4;
    z-index: 2;
    cursor: auto;
}

.decorate-cube .cube-selected-text {
    font-size: 20px;
    width: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    text-align: center;
    color: #88c4dc;
    font-size: 12px;
}

// 删除
.cube-selected-del {
    text-align: center;
    width: 12px;
    height: 12px;
    color: #f3704b;
    line-height: 9px;
    font-size: 2px;
    border-radius: 50%;
    border: 0.5px solid #f3704b;
    position: absolute;
    top: 0px;
    right: 5px;
    display: none;
}

.cube-selected:hover .cube-selected-del {
    display: block;
}


.decorate-cube .cube-row {
    float: left;
    list-style: none;
    padding: 0;
    margin: 0;
}

.decorate-cube .cube-item:first-child {
    border-top: 1px solid #e5e5e5;
}

.decorate-cube .cube-item {
    background: #f8f8f8;
    border-left: 1px solid #e5e5e5;
    border-bottom: 1px solid #e5e5e5;
    cursor: pointer;
    text-align: center;
    box-sizing: border-box;
}

.decorate-cube .cube-item.item-selecting {
    background: #e0edff;
}

.decorate-cube .cube-item.item-selected {
    background: #e0edff;
    visibility: hidden;
}
</style>
